package eval.utilities.random;

import java.util.Date;

import cern.jet.random.AbstractDistribution;
import cern.jet.random.Normal;
import cern.jet.random.Poisson;
import cern.jet.random.Uniform;
import cern.jet.random.engine.MersenneTwister;
import cern.jet.random.engine.RandomEngine;
import eval.utilities.configuration.ExperimentConfiguration;

public class RandomUtilities {

	private static final ExperimentConfiguration conf = ExperimentConfiguration
			.getSingleton();

	public static RandomEngine randomEngine = null;
	
	public static Uniform uniformDistribution = null;

	public static Uniform getNewUniformDistribution() {
		return new Uniform(getNewRandomEngine());
	}

	public static Uniform getNewUniformDistribution(double min, double max) {
		return new Uniform(min, max, getNewRandomEngine());
	}

	public static Poisson getNewPoissonDistribution(double average) {
		return new Poisson(average, getNewRandomEngine());
	}

	/**
	 * Returns a random number between 0 and 1 extracted from a uniform
	 * distribution
	 * */
	public static double generateNewRandomNumber() {
		if (uniformDistribution == null)
			uniformDistribution = getNewUniformDistribution();
		return uniformDistribution.nextInt();
	}

	public static RandomEngine getNewRandomEngine() {
		if (randomEngine == null) {
			if (conf.randomSeed > 0)
				randomEngine = new MersenneTwister(conf.randomSeed);
			else
				randomEngine = new MersenneTwister(new Date());
		}
		return randomEngine;
	}

	public static Normal getNewNormalDistribution(int mean, double std) {
		return new Normal(mean, std, getNewRandomEngine());
	}

	public static AbstractDistribution getNewConstantDistribution(int average) {
		return new ConstantDistribution(average);
	}

}
